import { useEffect } from 'react';
import { useRoutes, useLocation, useNavigate } from 'react-router-dom';
import router from "./router";
import { message } from 'antd';
// 去往登录页
function ToLogin(){
  const navigate = useNavigate()
  useEffect(() => {
    navigate("/login");
    message.warning('您还没有登录，请先登录！');
  },[])
  return <div></div>
}

// 去往首页
function ToPage(){
  const navigate = useNavigate()
  useEffect(() => {
    navigate("/page1");
    message.success('您已经登录了！');
  },[])
  return <div></div>
}

function BeforeRouterEnter(){
  const outlet = useRoutes(router);
  const location = useLocation();
  const token = localStorage.getItem('token');
  if(location.pathname === '/login' && token){
    return <ToPage />
  }
  if(location.pathname !== '/login' && !token){
    return <ToLogin />
  }
  return outlet;
}
function App() {
  return (
    <>
      <div className='App'>
        <BeforeRouterEnter />
      </div>
    </>
  )
}

export default App
